﻿using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Spark.AspNetCore;
using Spark.Config.Api.AppCode;
using Spark.Config.Api.AppCode.Sms;
using Spark.Config.Api.Services.Abstractions;
using Spark.Config.Api.Services.Implements;
using Spark.Elasticsearch;
using Spark.SmartSqlConfig;
using Spark.Swagger;
using Swashbuckle.AspNetCore.Swagger;

namespace Spark.Config.Api
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAutoMapper(y => { y.AddProfile<MappingProfile>(); });

            //添加火花
            services.AddSpark(builder =>
            {
                builder
                    .AddAuthentication(Configuration)
                    .AddJwtHandler(Configuration)
                    .AddCors()
                    .AddElasticsearch(Configuration)
                    .AddSmsService(option =>
                    {
                        option.IsDebug = false;
                        option.YunXinShiSMS = new Yunxinshisms
                        {
                            Pass = "89b0692932a496d8059686cfe74a1d21",
                            Uid = "cdxiaosheng",
                            Url = "http://api.sms.cn/sms/"
                        };
                    })
                    .AddSwagger(option =>
                    {
                        option.Add("v1", new Info { Title = "Spark", Version = "v1", Description = "https://guodaxia.com/" });
                    }, "Spark.Config.Api")
                    .AddSmartSql("Spark.Config.Api"); //添加SmartSql数据库支持

                builder.Services.AddCors(options => options.AddPolicy("CorsPolicy",
                    b => b.AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials()));

                builder.Services.AddMvc()
                    .AddJsonOptions(options =>
                    {
                        options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                        options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                    })
                    .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

                //.AddEventBus(x => { }) //添加消息总线
                //.AddLog(x => x.UseEventBusLog(Configuration));
            });

            RegisterService(services);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            //if (env.IsDevelopment())
            //{
            app.UseDeveloperExceptionPage();
            //}

            app
                .UseSpark()
                .UseCors("CorsPolicy")
                .UseMvc(routes => routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"))
                .UseSwagger()
                .UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "SparkAPI"); });
        }

        private void RegisterService(IServiceCollection services)
        {
            services.AddTransient<IAccountServices, AccountServices>();
            services.AddTransient<IAppServices, AppServices>();
            services.AddTransient<IUserServices, UserServices>();
            services.AddTransient<ISmsServices, SmsServices>();
            services.AddTransient<IConfigServices, ConfigServices>();
            services.AddTransient<IApiServices, ApiServices>();
            services.AddSingleton<IPower, Power>();
        }
    }
}